package cn.fql.order.service.impl;

import cn.fql.basic.util.LoginContext;
import cn.fql.basic.util.PageList;
import cn.fql.order.domain.OrderPetAcquisition;
import cn.fql.order.mapper.OrderPetAcquisitionMapper;
import cn.fql.order.query.OrderPetAcquisitionQuery;
import cn.fql.order.service.IOrderPetAcquisitionService;
import cn.fql.basic.service.impl.BaseServiceImpl;
import cn.fql.org.domain.Employee;
import cn.fql.org.domain.Shop;
import cn.fql.org.mapper.EmployeeMapper;
import cn.fql.org.mapper.ShopMapper;
import cn.fql.pet.domain.Pet;
import cn.fql.pet.domain.SearchMasterMsg;
import cn.fql.pet.dto.OrderDto;
import cn.fql.pet.mapper.PetMapper;
import cn.fql.pet.mapper.SearchMasterMsgMapper;
import cn.fql.user.domain.Logininfo;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public PageList<OrderPetAcquisition> queryList(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        //1.获取当前登陆人的信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //2.获取管理员Employee的信息
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //如果是店铺管理员或者店员的话 shop_id就不是空的
        if(employee.getShop_id() != null){
            //设置店铺的id  店铺管理员只能查询出自己店铺的订单
            query.setShopId(employee.getShop_id());
            Shop shop = shopMapper.findById(employee.getShop_id());
            //如果店铺管理员id 和当前登录人的id不一样 那就不是店长
            if(shop.getAdminId() != employee.getId()){
                //店员只能查看自己接单的宠物 不能查询出其他的订单
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        //orderId 支付的方式 成交价
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(orderDto.getId());
        SearchMasterMsg msg = searchMasterMsgMapper.findById(order.getSearchMasterMsgId());
        Pet pet = msg2Pet(msg,orderDto.getMoney());
        petMapper.save(pet);

        //2.改订单状态为1，价格为成交价，支付方式
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);

        //改寻主消息状态为2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }

    private Pet msg2Pet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    public void del(Long id) {
        //将寻主信息的放入寻主池  就是把 state=3 和shopid=null
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(id);
        //把id传给reject进行更改状态和店铺的id
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //将订单的信息状态更改
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }
}
